package com.cheeven.cwliu.citation;

import com.cheeven.cwliu.citation.steps.Step0;
import com.cheeven.cwliu.citation.steps.Step1;
import com.cheeven.cwliu.citation.steps.Step10;
import com.cheeven.cwliu.citation.steps.Step2;
import com.cheeven.cwliu.citation.steps.Step2_1;
import com.cheeven.cwliu.citation.steps.Step2_2;
import com.cheeven.cwliu.citation.steps.Step2_3;
import com.cheeven.cwliu.citation.steps.Step3;
import com.cheeven.cwliu.citation.steps.Step4;
import com.cheeven.cwliu.citation.steps.Step5;
import com.cheeven.cwliu.citation.steps.Step6;
import com.cheeven.cwliu.citation.steps.Step7;
import com.cheeven.cwliu.citation.steps.Step8;
import com.cheeven.cwliu.citation.steps.Step9;
import java.util.HashMap;
import java.util.HashSet;

public class App {

	private static final int MAX_MARKED_LIST_SIZE = 5000;
	private static final int MAX_CSV_FILE_SIZE = 500;

	public static void main(String[] args) {
		
		HashMap<String, String> control = new HashMap<String, String>();
		
		// 0. Load application parameters
		Step0.loadApplicationParameters(control);

		if(!control.containsKey("cwliu.citation.journal.name")) {
			control.put("cwliu.citation.journal.name", args[0]);
		}

		// 1. Load the first page and create the session
		Step1.establishHttpSession(control);

		// 2. Submit the Search Form
		Step2.searchArticles(control);

		HashSet<String> documentTypes = Step2_1.getDocumentTypes(control);

		for (String documentType : documentTypes) {

			control.put("documentType", documentType);
			
			Step2_2.refineSearch(control);
			
			int markFrom = 1;
			int markTo = MAX_MARKED_LIST_SIZE;
			while (true) {
				control.put("markFrom", String.valueOf(markFrom));
				control.put("markTo", String.valueOf(markTo));

				// 3. Select all references as Marked Record
				Step3.markRecords(control);

				// 4. Go to Marked List page
				Step4.openMarkedListPage(control);

				// 5. Open Web of Science page
				Step5.openWebOfSciencePage(control);

				int mark_from = 1;
				int mark_to = MAX_CSV_FILE_SIZE;
				
				int hitCount = Integer.parseInt(control.get("hitCount"));
				while (true) {

					mark_to = Math.min(mark_to, hitCount);
					control.put("mark_from", String.valueOf(mark_from));
					control.put("mark_to", String.valueOf(mark_to));

					// 6. Open Citation Report page
					Step6.openCitationReport(control);

					// 7. Request CSV Files
					Step7.requestCSVFile(control);

					// 8. Download CSV Files
					Step8.downloadCSVFile(control);

					if (mark_to == hitCount) {
						break;
					}

					mark_from += MAX_CSV_FILE_SIZE;
					mark_to += MAX_CSV_FILE_SIZE;
				}

				// 9. Return to Request CSV Page
				Step9.clearMarkedList(control);

				if (hitCount < MAX_MARKED_LIST_SIZE) {
					break;
				}

				markTo += MAX_MARKED_LIST_SIZE;
				markFrom += MAX_MARKED_LIST_SIZE;

			}
			
			Step2_3.exitFromRefineSearch(control);
		}
		
		Step10.logoutHttpSession(control);
	}
}
